package com.frxuexi.mdm_2207

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.frxuexi.mdm_2207.myview.MyProgressView
import com.frxuexi.mdm_2207.ui.theme.MDM2207Theme
import com.frxuexi.mdm_2207.view.MainPage
import com.frxuexi.mdm_2207.view.WelcomePage
import com.frxuexi.mdm_2207.view.url
import com.tencent.mmkv.MMKV
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

lateinit var activity: MainActivity
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @SuppressLint("CoroutineCreationDuringComposition")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activity=this
        requestPermissions(arrayOf("android.permission.READ_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE"),100)
        setContent {
            MDM2207Theme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    val kv = MMKV.defaultMMKV()
                    val preTime = kv.decodeLong("pre",0)
                    val currentTime = System.currentTimeMillis()
                    var startPage="welcome"
                    if(currentTime-preTime<24*60*60*1000){ //直接进入主页
                        startPage="main"
                    }
                    //1.创建 NavController
                    val navHostController = rememberNavController()
                    NavHost(navController = navHostController, startDestination = startPage, builder = {
                        composable("welcome"){ WelcomePage(navHostController)}
                        composable("main"){ MainPage()}
                    })

                    /*AndroidView(factory = {
                        val webView = WebView(it)
                        webView.loadUrl("https://baijiahao.baidu.com/s?id=1819774975220795028&wfr=spider&for=pc")
                        webView
                    })*/

                    /*lifecycleScope.launch {
                        listOf<Int>(3,4,5).asFlow().collect{
                            Log.i("===",it.toString())
                        }

                        flow<Int> {
                            delay(3000)
                            emit(10)
                        }.buffer().collect{
                            Log.i("===",it.toString())
                        }
                    }*/



                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            val bundle = data?.extras//拿到相册返回的bundle
            val bean = bundle?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)//再通过图片拾取器提供key拿到对应的bean
            val filePath = bean?.path
            if (filePath != null) {
                url =filePath
                Log.i("===",url)
            }
        }
    }
}





